package br.com.felix.dbm.ui.components;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JTextField;

/**
 * Key Adapter that converts spaces to underlines and puts all characters in
 * upper case.
 * */
public class DatabaseNameKeyAdapter extends KeyAdapter {
	protected String databaseName = "";



	public DatabaseNameKeyAdapter() {

	}



	@Override
	public void keyReleased(KeyEvent e) {
		e.consume();
		JTextField txtDatabaseName = (JTextField) e.getSource();
		int caret = txtDatabaseName.getCaretPosition();
		if (!e.isActionKey()) {
			int start = txtDatabaseName.getSelectionStart();
			int end = txtDatabaseName.getSelectionEnd();
			databaseName = txtDatabaseName.getText();
			databaseName = databaseName.toUpperCase().replace(' ', '_');
			txtDatabaseName.setText(databaseName);
			txtDatabaseName.setSelectionStart(start);
			txtDatabaseName.setSelectionEnd(end);
			txtDatabaseName.setCaretPosition(caret);
			afterKeyReleased();
		}
	}



	/**
	 * Called after a key (that is not an action key) is released.
	 * */
	public void afterKeyReleased() {

	}
}
